import type {RequestParams} from '@/constrain/interface'
const BASEURL = 'https://gmall-prod.atguigu.cn/skb'
class Service{
    // private 只能在当前类使用 派生类不能用  protected 允许派生类用
    private api(params:RequestParams):any{
        return new Promise((resolve,reject) => {
            //提示用户
            uni.showLoading({
                title:'正在加载'
            })

            const token = uni.getStorageSync('token')
            type headerType = {
                'Content-type':string,
                token?:string
            }
            const header:headerType = {
                'Content-type':'application/json'
            }
            token && (header.token = token)

            //执行异步任务
            uni.request({
                url:BASEURL + params.url,
                data:params.data,
                method:params.method,
                header,
                success:(res) => {
                    //修改状态,传递结果
                    resolve(res.data)
                },
                fail:(error) => {
                    reject(error)
                },
                complete:() => {//成功失败都会执行
                    //关闭正在加载的提示
                    uni.hideLoading()
                }
            })
        })
    }
    get(params:RequestParams){
        params.method = 'GET'
        return this.api(params)
    }
    post(params:RequestParams){
        params.method = 'POST'
        return this.api(params)
    }
    put(params:RequestParams){
        params.method = 'PUT'
        return this.api(params)
    }
    delete(params:RequestParams){
        params.method = 'DELETE'
        return this.api(params)
    }
}
export default Service